package com.yanqu.road.server.cmd.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionwar.UnionWarSessionReward;
import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarDetail;
import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarSimple;
import com.yanqu.road.entity.activity.unionwar.cross.UnionWarPatronsData;
import com.yanqu.road.entity.enums.eUnionWarWinMark;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.logic.pb.UnionWarActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarActivityMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = CrossProtocol.C_UNION_WAR_PREPARE_OVERVIEW, desc = "备战概览")
public class CrossUnionWarPrepareOverviewCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        CrossUnionWarProto.CrossPrepareOverviewReqMsg reqMsg = CrossUnionWarProto.CrossPrepareOverviewReqMsg.parseFrom(packet.getBytes());
        ActivityInfo activityInfo = CrossUnionWarActivityMgr.getOpenActivity(reqMsg.getActivityId());
        String unionUid = Cross2UnionActivityGroupMgr.getUnionUid(reqMsg.getActivityId(), packet.getPlayerId());

        UnionWarActivityServerProto.PrepareOverviewRespMsg.Builder respMsg = UnionWarActivityServerProto.PrepareOverviewRespMsg.newBuilder();
        respMsg.setRet(0);
        if(StringUtils.isNullOrEmpty(unionUid)){
            respMsg.setRet(GameErrorCode.E_UNION_NO_JOIN_UNION);
        }
        if(activityInfo == null){
            respMsg.setRet(GameErrorCode.E_ACTIVITY_OVER_TIME);
        }
        if(respMsg.getRet() != 0){
            respMsg.setActivityId(activityId);
            respMsg.setPhase(reqMsg.getPhase());
            respMsg.setBeginSession(reqMsg.getBeginSession());
            respMsg.setEndSession(reqMsg.getEndSession());
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_WAR_PREPARE_OVERVIEW, respMsg));
            return;
        }

        int endSession = reqMsg.getEndSession();
        int beginSession = reqMsg.getBeginSession();
        if(endSession == 0){
            endSession = CrossUnionWarActivityMgr.getAllSession(activityId, reqMsg.getPhase());
        }
        if(beginSession == 0){
            beginSession = 1;
        }

        respMsg.setActivityId(activityId);
        respMsg.setPhase(reqMsg.getPhase());
        respMsg.setBeginSession(beginSession);
        respMsg.setEndSession(endSession);
        respMsg.setRet(0);

        List<CrossUnionWarDetail> list = new ArrayList<>();


        for (int i = beginSession; i <= endSession; i++){
            CrossUnionWarDetail detail = CrossUnionWarMgr.getCrossUnionWarDetailReadOnly(reqMsg.getActivityId(), reqMsg.getPhase(), i, unionUid);
            if(detail == null){
                continue;
            }
            list.add(detail);
        }

        if(list.size() > 0){
            UnionActivityGroupUnion groupUnion = Cross2UnionActivityGroupMgr.getUnionData(activityId, packet.getPlayerId());
            CrossUnionWarSimple simple = CrossUnionWarMgr.getCrossUnionWarSimple(activityId, unionUid);
            UnionBaseInfo unionInfo = null;
            if(simple != null){
                unionInfo = simple.getUnionBaseInfo();
            }
            for(CrossUnionWarDetail detail : list){
                UnionBaseInfo enemy = null;
                CrossUnionWarSimple enemySimple = CrossUnionWarMgr.getCrossUnionWarSimple(activityId, detail.getEnemyUnionUid());
                if(enemySimple != null) {
                    enemy = enemySimple.getUnionBaseInfo();
                }
                UnionWarActivityServerProto.SessionLineupTemp.Builder tempBuilder = UnionWarActivityPb.parseSessionLineupTempList(detail, false, groupUnion.getUserMap().size(), enemy, unionInfo);
                tempBuilder.setSend(false);
                for (int i = 0; i < detail.getPatronsDataList().size(); i++) {
                    UnionWarPatronsData patronsData = detail.getPatronsDataList().get(i);
                    if(patronsData.getUserId() == packet.getPlayerId()){
                        tempBuilder.setSend(true);
                    }
                }
                if(detail.getWinMark() == eUnionWarWinMark.Bye.getValue() && detail.getStageNo() == 2){
                    //UnionWarSessionReward reward = player.getModule(UnionWarActivityModule.class).getBattleReward(activityInfo.getActivityId(), detail.getPhase(), detail.getSession());
                    //if(reward == null){
                    tempBuilder.setGetByeReward(false);
                    for (CrossUnionWarProto.SessionGetRewardInfoMsg msg : reqMsg.getGetRewardList()) {
                        if(detail.getRoundNo() == msg.getSession()){
                            tempBuilder.setGetByeReward(true);
                        }
                    }
                }
                respMsg.addLineupList(tempBuilder);
            }
        }

        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_WAR_PREPARE_OVERVIEW, respMsg));
    }

    @Override
    public void sendData() {

    }
}
